import axios from 'axios'
import commonUtil from '@/assets/js/commonUtil.js'
axios.defaults.timeout = 60000
let SERVER = '',
    treeUrl,
    callText
if (process.env.NODE_ENV === 'development') {
  // 开发
  SERVER = `https://nhousehiddencall.allhome.com.cn`
  treeUrl = 'https://hsl.allhome.com.cn/'
  callText = 'https://voice.allhome.com.cn/'
} else if(process.env.NODE_ENV === 'pred') {
  // 加测试网关
  SERVER = 'https://dgateway.allhome.com.cn/hiddencallmini/'
  treeUrl = 'https://dgateway.allhome.com.cn/organization/'
  callText = 'https://dgateway.allhome.com.cn/voicetotext/'
} else if(process.env.NODE_ENV === 'lan') {
  // 200
  // SERVER = 'https://dgateway.allhome.com.cn/hiddencallmini/'
  // treeUrl = 'https://dgateway.allhome.com.cn/organization/'
} else {
  SERVER = `https://nhousehiddencall.allhome.com.cn`
  treeUrl = 'https://hsl.allhome.com.cn/'
  callText = 'https://voice.allhome.com.cn/'
}
axios.defaults.baseURL = SERVER
/**
 * 请求拦截
 * @author 王磊
 * @date 2019-4-1
 */
axios.interceptors.request.use(config => {
  // 请求时是否有统一登录token，有的话存储
  if (window.token) {
    config.headers.token = window.token
  }
  return config
}, err => {
    return Promise.reject(err)
  }
)
/**
 * 响应拦截
 * @author 王磊
 * @date 2019-2-15
 */
axios.interceptors.response.use((res) => {
  //token失效
  if(res.data.statusCode === 140501) {
    commonUtil.setMessage('error', '请重新登录！')
    setTimeout(() => {
      window.logout()
    }, 2500)
    return
  }
  // 如果有响应token存储，发送给统一登录
  if (res.headers.pragma) {
    window.token = res.headers.pragma
  }
  return res
}, (error) => {
  return Promise.reject(error)
})
export default {
  axios,
  treeUrl,
  callText
}